import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import arkin.ArkinPackage;
import arkin.Robot;


public class Generator {

	
	public static Robot load(File f) {
		ResourceSet rs = new ResourceSetImpl();
		Resource.Factory.Registry registry =
		rs.getResourceFactoryRegistry();
		Map<String,Object> m = registry.getExtensionToFactoryMap();
		m.put("xmi", new XMIResourceFactoryImpl());
		rs.getPackageRegistry().put(ArkinPackage.eNS_URI,
		ArkinPackage.eINSTANCE);
		URI uri = URI.createFileURI(f.getAbsolutePath());
		Resource resource = rs.getResource(uri, true);
		if (resource.isLoaded() && resource.getContents().size() > 0) {
		return (Robot) resource.getContents().get(0);
		}
		return null;
		}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Robot robot = load(new File(args[0]));
		ArkinTemplate template = new ArkinTemplate();
		Visitor visitor = new Visitor(template);
		visitor.visit(robot);
		try {
			template.write();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
